package com.wxb.shmily.redis;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by admin on 2017/6/9.
 */
public enum RedisCacheConfiguration {
    REDISFACTORY;

    private final String host = "127.0.0.1";

    private final int port = 6379;

    private final int timeout = 10000;

    private final int maxIdle = 8;

    private final int minIdle = 1;

    private final long maxWaitMillis = 6000;

    private final int maxTotal = 20;

    private final String password = "wxb1993";

    private JedisPool jedisPool = null;

    private RedisCacheConfiguration(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //最大空闲连接数
        jedisPoolConfig.setMaxIdle(maxIdle);
        //最小空闲连接数
        jedisPoolConfig.setMinIdle(minIdle);
        //最大连接数
        jedisPoolConfig.setMaxTotal(maxTotal);
        //获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        //逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
        jedisPoolConfig.setMinEvictableIdleTimeMillis(1800000);
        //获得一个jedis实例的时候是否检查连接可用性（ping()）
        jedisPoolConfig.setTestOnBorrow(true);
        //return 一个jedis实例给pool时，是否检查连接可用性（ping()）
        jedisPoolConfig.setTestOnReturn(true);
        //idle状态监测用异步线程evict进行检查
        jedisPoolConfig.setTestWhileIdle(true);
        //一次最多evict的pool里的jedis实例个数
        jedisPoolConfig.setNumTestsPerEvictionRun(10);
        jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
    }

    public JedisPool getInstance(){
        return jedisPool;
    }
}
